package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice//控制器增强注解
@Log4j2
public class ExceptionCatch {

    //捕获exception此类异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(Exception exception) {
        //输出异常
        exception.printStackTrace();
        //记录日志
        log.error("catch one error {}"+exception.getMessage());
        //返回异常后的结果
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "服务器维护");
    }

    //捕获CostomException此类异常
    @ExceptionHandler(CostomException.class)
    @ResponseBody
    public ResponseResult exception(CostomException e) {
        //输出异常
        e.printStackTrace();
        //记录日志
        log.error("catch one error {}"+e.getMessage());
        //返回异常后的结果
        return e.getResponseResult();
    }
}
